package com.help.tools;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
/**
 * 调用的推送服务实际放在59.69.101.29这台服务器上，为什么不放在http://helpmeservice.duapp.com？
 * 因为http://helpmeservice.duapp.com所在的IP为百度蜘蛛ip，貌似被友盟禁了，添加IP地址白名单没用
 * ***/
public class PushSeviceOnTeaPC {
	private static final String url="http://59.69.101.29:8080/PushDemo/PushServlet";
	//private static final String url="http://localhost:8081/PushDemo/PushServlet";
	public static String pushToAllService(String ticker,String title,String text) throws Exception, IOException{
		
			   // String url="http://helpmeservice.duapp.com/PushServlet";
		      // String url="http://localhost:8081/HelpServiceDemo/PushServlet";
			  // String url="http://localhost:8081/PushDemo/PushServlet";
			
		        HttpPost httpPost = new HttpPost(url); 
		        HttpParams httpParameters = new BasicHttpParams();  
		        HttpConnectionParams.setConnectionTimeout(httpParameters, 10*1000);//设置请求超时10秒  
		        HttpConnectionParams.setSoTimeout(httpParameters, 10*1000); //设置等待数据超时10秒  
		        HttpConnectionParams.setSocketBufferSize(httpParameters, 8192);  
		        HttpClient httpclient = new DefaultHttpClient(httpParameters); //此时构造DefaultHttpClient时将参数传入 
		        HttpClient client = new DefaultHttpClient();  
		        List<NameValuePair> valuePairs = new ArrayList<NameValuePair>();  
		        valuePairs.add(new BasicNameValuePair("action", "pushTest"));
		        valuePairs.add(new BasicNameValuePair("ticker", ticker)); 
		        valuePairs.add(new BasicNameValuePair("title", title));
		        valuePairs.add(new BasicNameValuePair("text", text));
		        
		        
		        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(valuePairs, "utf-8");  
		        httpPost.setEntity(formEntity);  
		        HttpResponse resp = client.execute(httpPost);  
		          
		        HttpEntity entity = resp.getEntity();  
		        String respContent = EntityUtils.toString(entity , "utf-8");  
		        httpPost.abort();  
		        client.getConnectionManager().shutdown();  
		        return respContent;
		   
		
	}

 
}
